#include "fatfs.h"
#include "sd.h"
#include "ff.h"
#include "diskio.h"
#include "stdio.h"
FATFS fatfs;
// #ifndef DEV_MMC
// #define DEV_MMC 0
// #else
// // #warning "DEV_MMC defined."
// #endif

void fatfs_init()
{
    int ret;
    disk_initialize(DEV_MMC);     // sdcard_init();替代
    ret = f_mount(&fatfs, "", 1); /* Mount option: 0=Do not mount (delayed mount), 1=Mount immediately */
    if (ret == 0)
    {
        printf("sd mount ok.\r\n");
    }
    else
    {
        printf("sd mount fail.\r\n");
    }
}

void fatfs_test()
{
    fatfs_init();
    FIL file;

    f_open(&file, "test.txt", FA_READ | FA_WRITE | FA_OPEN_ALWAYS);

    char txt[] = "test fatfs.\n";
    UINT wt;
    f_write(&file, txt, sizeof(txt), &wt);

    char read[sizeof(txt)];
    f_lseek(&file, 0);
    f_read(&file, read, sizeof(read), &wt);
    printf("\r\nfatfs read test.txt:%s", read);

    f_close(&file);
}